/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package df.bean.guarantee;

import df.bean.db.conn.DBConn;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author nopphadon
 */
public class ProcessGuaranteeRollbackBean {
    DBConn c;
    String err_mesg;
    public ProcessGuaranteeRollbackBean(DBConn db){
        c = db;
        try{
            c.setStatement();
        }catch(Exception e){
            System.out.println("Exception Set Statement from GuaranteeRollbackBean Class");
        }
    }
    public String getErrorMessage(){
        return this.err_mesg;
    }

    public boolean rollBackSetup(String hospital_code, String year, String month){
        boolean st = true;
        String sq = "UPDATE STP_GUARANTEE SET DF406_CASH_AMOUNT = 0, DF406_CREDIT_AMOUNT = 0, "+
        "DF406_HOLD_AMOUNT = 0, DF402_CASH_AMOUNT = 0, DF402_CREDIT_AMOUNT = 0, DF402_HOLD_AMOUNT = 0, "+
        "DF400_CASH_AMOUNT = 0, DF400_CREDIT_AMOUNT = 0, DF400_HOLD_AMOUNT = 0, DF_CASH_AMOUNT = 0, "+
        "DF_HOLD_AMOUNT = 0, HP402_ABSORB_AMOUNT = 0, DF406_ABSORB_AMOUNT = 0, DF402_ABSORB_AMOUNT = 0, "+
        "DF400_ABSORB_AMOUNT = 0, OVER_GUARANTEE_AMOUNT = 0, SUM_DR_OVER_AMOUNT = 0, DF_ABSORB_AMOUNT = 0, "+
        "SUM_HP_OVER_AMOUNT = 0, GUARANTEE_PAID_AMOUNT = 0, SUM_TAX_406 = 0, SUM_TAX_402 = 0, SUM_TAX_400 = 0, "+
        "START_DATE = OLD_START_DATE, END_DATE = OLD_END_DATE, HP_ABSORB_DOCTOR_CODE = '', "+
        "ACTIVE = OLD_ACTIVE, GUARANTEE_AMOUNT = OLD_GUARANTEE_AMOUNT, OLD_ABSORB_AMOUNT = 0, " +
        "GUARANTEE_FIX_AMOUNT = OLD_GUARANTEE_FIX_AMOUNT, "+
        "GUARANTEE_INCLUDE_AMOUNT = OLD_GUARANTEE_INCLUDE_AMOUNT, "+
    	"GUARANTEE_EXCLUDE_AMOUNT = OLD_GUARANTEE_EXCLUDE_AMOUNT, " +
    	"SPECIAL_AMOUNT = OLD_SPECIAL_AMOUNT "+
        "WHERE HOSPITAL_CODE = '"+hospital_code+"' AND YYYY = '"+year+"' AND MM = '"+month+"'";
        try {
            System.out.println("Start Rollback Guarantee");
            c.insert(sq);
            c.insert("UPDATE STP_GUARANTEE SET DF_ABSORB_AMOUNT = OLD_ABSORB_AMOUNT "+
            		 "WHERE HOSPITAL_CODE = '"+hospital_code+"'");
            //System.out.println("Rollback Setup Guarantee DF_ABSORB_AMOUNT = OLD_ABSORB_AMOUNT");
            c.insert("DELETE FROM SUMMARY_GUARANTEE WHERE MM = '"+month+"' AND YYYY = '"+
                      year+"' AND HOSPITAL_CODE = '"+hospital_code+"'");
            //System.out.println("Rollback Guarantee Expense = OLD_ABSORB_AMOUNT");
            c.insert("DELETE FROM TRN_EXPENSE_DETAIL WHERE MM = '"+month+"' AND YYYY = '"+
                      year+"' AND HOSPITAL_CODE = '"+hospital_code+"' AND EMPLOYEE_ID = 'ProcessGuarantee'");
            c.commitDB();
        } catch (SQLException ex) {
            System.out.println("Error Mes From rollBackSetup : "+ex);
            err_mesg = ""+ex;
            st = false;
        } finally{
        	System.out.println("Finish rollback guarantee");
        }
        return st;
    }
    
    public boolean rollBackTransaction(String hospital_code, String year, String month){
        boolean st = true;
        String mess = "";
        String set_order_item = "UPDATE TRN_DAILY SET ORDER_ITEM_ACTIVE = '0'" +
                     " WHERE TRANSACTION_DATE LIKE '"+year+""+month+"%' "+
                     "AND HOSPITAL_CODE = '"+hospital_code+"'";

        String set_transaction = "UPDATE TRN_DAILY SET GUARANTEE_PAID_AMT = 0, "+
        "GUARANTEE_AMT = CASE WHEN COMPUTE_DAILY_USER_ID NOT LIKE 'ALLOC%' " +
        "THEN 0 ELSE GUARANTEE_AMT END, "+
        "GUARANTEE_CODE = '', GUARANTEE_DR_CODE = '', GUARANTEE_TYPE = '', " +
        "GUARANTEE_DATE_TIME = '', GUARANTEE_TERM_MM = '', GUARANTEE_TERM_YYYY = '', "+
        "GUARANTEE_NOTE = '', IS_GUARANTEE = '', IS_PAID = 'Y', DR_AMT = OLD_DR_AMT, "+
        "HP_AMT = AMOUNT_AFT_DISCOUNT - OLD_DR_AMT, "+
        "DR_TAX_400 = CASE WHEN TAX_TYPE_CODE = '400' THEN OLD_TAX_AMT ELSE '0' END, "+
        "DR_TAX_401 = CASE WHEN TAX_TYPE_CODE = '401' THEN OLD_TAX_AMT ELSE '0' END, "+
        "DR_TAX_402 = CASE WHEN TAX_TYPE_CODE = '402' THEN OLD_TAX_AMT ELSE '0' END, "+
        "DR_TAX_406 = CASE WHEN TAX_TYPE_CODE = '406' THEN OLD_TAX_AMT ELSE '0' END "+
        "WHERE TRANSACTION_DATE LIKE '"+year+month+"%' " +
        "AND HOSPITAL_CODE = '"+hospital_code+"'";
        
        String set_advance = "UPDATE TRN_DAILY SET " +
        "YYYY = '', "+
        "MM = '', "+
        "PAY_BY_CASH = 'N', "+
        "RECEIPT_NO = '', "+
        "RECEIPT_DATE = '' "+
        "WHERE RECEIPT_NO = 'ADVANCE' AND YYYY = '"+year+"' AND MM = '"+month+"'";

        try {
        	mess = set_order_item;
            c.insert(set_order_item);
            mess = set_transaction;
            c.insert(set_transaction);
            mess = set_advance;
            c.insert(set_advance);
            c.commitDB();
            System.out.println("Rollback Transaction Guarantee Complete");
        } catch (SQLException ex) {
            System.out.println("Error Mes From rollBackTransaction : "+ex);
            System.out.println(mess);
            err_mesg = ""+ex;
            st = false;
        }
        return st;
    }
}